/**
 * ThirdPartyQueryInvokeService.java
 * author: yujiakui
 * 2017年8月28日
 * 下午4:57:31
 */
package com.ctim.service.integration.pengyuan;

import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;

import com.ctim.common.exception.PcsRunTimeException;

/**
 * @author yujiakui
 *
 *         下午4:57:31
 *
 *
 *         第三方查询调用服务
 */
public class ThirdPartyQueryInvokeService {

	/**
	 *
	 * 统一调用外部第三方接口
	 *
	 * @param url
	 * @param queryInterfaceName
	 * @param params
	 * @return
	 * @throws MalformedURLException
	 * @throws Exception
	 */
	public static Object[] invoke(String url, String queryInterfaceName, Object[] params) {

		StringBuilder interaceLogInfo = new StringBuilder("调用第三方接口异常，url=");
		interaceLogInfo.append(url).append(";queryInterfaceName=");
		interaceLogInfo.append(queryInterfaceName);

		URL urlObj;
		try {
			urlObj = new URL(url);
		} catch (MalformedURLException e) {

			throw new PcsRunTimeException(interaceLogInfo.toString() + ",URL生成异常", e);
		}

		// 调用webservice
		Client client;
		try {
			client = new Client(urlObj);
		} catch (Exception e) {
			throw new PcsRunTimeException(interaceLogInfo.toString() + ",Client生成异常", e);
		}

		// 调用外部接口获得对应的结果
		Object[] result = null;
		try {
			result = client.invoke(queryInterfaceName, params);
		} catch (Exception e) {
			throw new PcsRunTimeException(interaceLogInfo.toString() + ",client.invoke异常", e);
		}

		if (null == result) {
			throw new PcsRunTimeException("调用第三方接口返回对应的接口是null");
		}
		return result;

	}
}
